package com.egao.common.publicly;

import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.publicly.entity.ArticleVO;
import com.egao.common.publicly.service.PubliclyArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 文章
 *
 * @author pyx
 */
@Api(tags = "文章")
@RestController
@RequestMapping("${api}")
public class PubliclyArticleController {

    @Resource
    private PubliclyArticleService publiclyArticleService;

    @ApiOperation("列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "categoryId", value = "所属栏目id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "标题", required = false, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/publicly/article/list")
    public PageResult<ArticleVO> list(HttpServletRequest request) {
        PageParam<ArticleVO> pageParam = new PageParam<>(request);
        return new PageResult<>(publiclyArticleService.listPage(pageParam), pageParam.getTotal());
    }


    @ApiOperation("详情栏目文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "所属栏目id", required = true, dataType = "int", paramType = "query"),
    })
    @GetMapping("/publicly/article/particular")
    public JsonResult particular(Integer categoryId) {
        if (categoryId == null) {
            return JsonResult.error("没有栏目编号");
        }

        ArticleVO article = publiclyArticleService.particular(categoryId);

        return JsonResult.ok().setData(article);
    }

    @ApiOperation("详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章id", required = true, dataType = "int", paramType = "query"),
    })
    @GetMapping("/publicly/article/get")
    public JsonResult get(Integer id) {
        if (id == null) {
            return JsonResult.error("没有文章id");
        }

        ArticleVO articleVO = publiclyArticleService.getId(id);
        if (articleVO == null) {
            return JsonResult.ok("文章不存在");
        }

        return JsonResult.ok().setData(articleVO);
    }


    @ApiOperation("公告栏列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "所属栏目id", required = true, dataType = "int", paramType = "query"),
    })
    @GetMapping("/publicly/article/notice/list")
    public JsonResult listNotice(HttpServletRequest request) {
        PageParam<ArticleVO> pageParam = new PageParam<>(request);
        return JsonResult.ok().setData(publiclyArticleService.listNotice(pageParam));
    }

    @ApiOperation("公告栏轮播图列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "所属栏目id", required = true, dataType = "int", paramType = "query"),
    })
    @GetMapping("/publicly/article/img/list")
    public JsonResult listImg(HttpServletRequest request) {
        PageParam<ArticleVO> pageParam = new PageParam<>(request);
        return JsonResult.ok().setData(publiclyArticleService.listImg(pageParam));
    }

}
